
export class Position {
    /**
     * 
     * @param {Number} x 
     * @param {Number} y 
     */
    constructor(x,y){
        this.x=x;
        this.y=y;
    }
}
/**
 * 
 * @param {RoomPosition} pos 
 * @param {DirectionConstant} direction 
 * @returns {RoomPosition}
 */
export function step(pos, direction){
    let {x,y,roomName}=pos;
    let offset;
    switch(direction){
        case TOP:
            offset = [ 0,-1];
            break;
        case TOP_RIGHT:
            offset = [ 1,-1];
            break;
        case RIGHT:
            offset = [ 1, 0];
            break;
        case BOTTOM_RIGHT:
            offset = [ 1, 1];
            break;
        case BOTTOM:
            offset = [ 0, 1];
            break;
        case BOTTOM_LEFT:
            offset = [-1, 1];
            break;
        case LEFT:
            offset = [-1, 0];
            break;
        case TOP_LEFT:
            offset = [-1,-1];
            break;
        default:
            offset = [ 0, 0];
            break;
    }
    x+=offset[0];
    y+=offset[1];

    for(;;){
        try{
            return new RoomPosition(x,y,roomName);
        }catch(e){

        }
        let exits= Game.map.describeExits(roomName);
        if(x>49){
            x-=50;
            roomName = exits[RIGHT];
        }else if(x<0){
            x+=50;
            roomName = exits[LEFT];
        }
        if(y>49){
            y-=50;
            roomName = exits[BOTTOM];
        }else if(y<0){
            y+=50;
            roomName = exits[TOP];
        }
    }
}

export function posIsEqual(a,b){
    return a.x === b.x && zip.y === b.y;
}
/**
 * 
 * @param {RoomPosition} pos 
 */
export function canMoveOn(pos){
    let room = Game.rooms[pos.roomName];
    if(!room){
        return true;
    }
    if(room.getTerrain().get(pos.x,pos.y) === TERRAIN_MASK_WALL){
        return false;
    }

    let look = pos.lookFor(LOOK_STRUCTURES,{
        filter(structure){
            if([STRUCTURE_CONTAINER,STRUCTURE_ROAD]
                    .some((type)=>structure.structureType === type)){
                return false;
            }
            if(structure.structureType === STRUCTURE_RAMPART && structure.my){
                return false;
            }
            return true;
        }
    });
    if(look.length>0){
        return false;
    }

    look = pos.lookFor(LOOK_CREEPS, {filter:(creep)=>!creep.my});
    if(look.length>0){
        return false;
    }

    return true;
}

/**
 * 
 * @param {Position} a 
 * @param {Position} b 
 * @param {String} roomName 
 * @returns {Position}
 */
export function posAdd(a,b, roomName = undefined){
    if(!roomName){
        roomName = a.roomName;
    }
    try{
        return new RoomPosition(a.x+b.x, a.y+b.y, roomName);
    }catch(e){
        return new Position(a.x+b.x, a.y+b.y);
    }
}

/**
 * 
 * @param {RoomPosition|Position} a 
 * @param {RoomPosition|Position} b 
 * @param {String} roomName 
 * @returns {Position|RoomPosition}
 */
export function posMid(a, b, roomName = undefined){
    if(!roomName){
        roomName = a.roomName;
    }
    let x = Math.floor((a.x+b.x)/2), y = Math.floor((a.y+b.y)/2);
    try{
        return new RoomPosition(x, y, roomName);
    }catch(e){
        return new Position(x,y);
    }
}

/**
 * 
 * @param {RoomPosition} pos 
 * @param {StructureConstant} STRUCTURE_TYPE
 * @returns {Boolean}
 */
export function lookStructureCreation(pos, STRUCTURE_TYPE){
    if(pos.lookFor(STRUCTURE_TYPE).length>0){
        return true;
    }
    for(let site of pos.lookFor(LOOK_CONSTRUCTION_SITES)){
        if(site.my && site.structureType === STRUCTURE_TYPE){
            return true;
        }
    }
    return false;
}